Refactor logging to follow Dependency Inversion Principle #20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Logging Subsystem Refactor — Dependency Inversion Compliance
Overview
This PR refactors the logging subsystem to resolve a Dependency Inversion Principle (DIP) violation.
Previously, the low-level
moshi.moshi.utils.loggingutility directly imported from the high-levelmoshi.moshi.client_utilsmodule. This created an architectural violation where foundational infrastructure depended on application-layer logic, resulting in tight coupling and a fragile dependency chain.This change introduces a clean abstraction and dependency injection to properly separate concerns and enforce layering.
Changes Summary
Architecture Separation
Introduced a
LoggingProviderabstraction via a PythonProtocol:The low-level
utilspackage now defines and owns the contract for the capabilities it requires.Decoupled Logging Utility
Refactored:
LoggingProviderinterfaceDependency Injection
Added a
configure_logging()registration mechanism that allows high-level modules to inject their implementation into the low-level infrastructure at runtime.Provider Implementation
Implemented
ClientLoggingProviderin:This satisfies the logging contract while preserving the existing terminal colorization behavior.
Explicit Initialization
Updated application entry points:
server.pyoffline.pyBoth now explicitly wire the logging implementation during startup via:
setup_client_logging()Technical Rationale
Dependency Inversion Principle (DIP) Compliance
Circular Import Prevention
Zero Behavioral Regressions
This refactor is strictly architectural with no functional behavior changes.
Verification Performed
Startup Testing
Verified:
Confirmed
offline.pyruns correctly with the new initialization flow.Runtime Validation
Using a bootstrap script, verified:
ColorizedLoguses the injected providerIsolation Check
Confirmed the logging utility can be imported independently:
This no longer pulls in
client_utilsor its dependencies.Files Modified
New
moshi/moshi/utils/logging_interface.pyUpdated
moshi/moshi/utils/logging.pymoshi/moshi/client_utils.pymoshi/moshi/server.pymoshi/moshi/offline.py